local skinlist = {}

--对局获取皮肤数据的函数
---@param general string @ 武将名
---@param kingdom string @ 势力
skinlist.getSkins = function(general, kingdom)
    local name = general
    local truename = Fk.generals[name].trueName
    local skins = {name}
    if skinlist.skinTable[kingdom] then
        if skinlist.skinTable[kingdom][name] then
            for _, n in pairs(skinlist.skinTable[kingdom][name]) do 
                table.insert(skins, n)
            end
        elseif skinlist.skinTable[kingdom][truename] then
            for _, n in pairs(skinlist.skinTable[kingdom][truename]) do 
                table.insert(skins, n)
            end
        end
    end
    return skins or {}
end

--皮肤翻译函数
---@param general string @ 武将名
---@param num integer @ 指定数量
---@param transName string @ 翻译结果
---@param skinname table @ 翻译目标
---@param extra table @ 额外信息
skinlist.skinTrans = function(general, num, transName, skinname, extra)
    if not skinname then
        skinname = {}
    end
    if extra then
        for i = 1, num do
            Fk:loadTranslationTable{["skin_"..extra..tostring(i).."__"..general] = transName}
            if skinname[i] then
                Fk:loadTranslationTable{["skin_"..extra..tostring(i)] = skinname[i]}
            end
        end
    else
        for i = 1, num do
            Fk:loadTranslationTable{["skin_"..general..tostring(i).."__"..general] = transName}
            if skinname[i] then
                Fk:loadTranslationTable{["skin_"..general..tostring(i)] = skinname[i]}
            end
        end
    end
end

--添加单个皮肤，不要使用
---@param general string
---@param table table
---@param num integer
---@param kingdom string
---@param gender Fk.Gender
skinlist.addskin = function(general, table, num, kingdom, gender)
    for i = 1, num do
        table[general..tostring(i)] = General(extension, "skin_"..general..tostring(i).."__"..general, kingdom, 0, 0, gender)
        table[general..tostring(i)].hidden = true
    end
end

--由索引值得到单个皮肤的字符串
---@param general string
---@param index integer
skinlist.getSkinStr = function(general, index)
    return "skin_"..general..tostring(index).."__"..general
end

--传入，传出对应皮肤的字符串
---@param general string
---@param skinsnum integer
---@param num integer
---@param extra table
skinlist.getAllSkinStr = function(general, skinsnum, num, extra)
    local skinlist = {}
    local n = skinsnum
    if num then
        n = num
    end
    if not extra then
        extra = general
    end
    for i = 1 , n do
        table.insert(skinlist, "skin_"..extra..tostring(i).."__"..general)
    end
    return skinlist
end

--将传入的特殊字符串分割成正常的表
---@param input string
---@param delimiter string
skinlist.split = function(input, delimiter)
    if not delimiter then
        delimiter = " "
    end
    input = tostring(input)
    delimiter = tostring(delimiter)
    if (delimiter == "") then return false end
    local pos, arr = 0, {}
    for st, sp in function() return string.find(input, delimiter, pos, true) end do
        table.insert(arr, string.sub(input, pos, st - 1))
        pos = sp + 1
    end
    table.insert(arr, string.sub(input, pos))
    return arr
end

--手动添加皮肤的函数，不要使用
---@param extension table?
---@param general string
---@param table table
---@param num integer
---@param kingdom string
---@param gender Fk.Gender
skinlist.addskins = function(extension, general, table, num, kingdom, gender)
    local kingdom = kingdom or "qun"
    local gender = gender or General.Female
    for i = 1, num do
        table[general..tostring(i)] = General(extension, "skin_"..general..tostring(i).."__"..general, kingdom, 0, 0, gender)
        table[general..tostring(i)].hidden = true
    end
end

--禁止某些武将使用皮肤
---@param general string or table
---@param kingdom string or table
skinlist.banGeneral = function(general, kingdom)
    if type(general) == "string" then
        general = {general}
    end
    if type(general) == "table" then
        for g in ipairs(general) do
            local kingdoms = {}
            if not kingdom then
                kingdoms = {"wei", "shu", "wu", "qun", "jin", "god"}
            elseif type(kingdom) == "table" then
                kingdoms = kingdom
            else
                kingdoms = {kingdom}
            end
            for k in ipairs(kingdoms) do
                skinlist.skinTable[k][g] = {}
            end
        end
    end
end

--一般皮肤添加方法，传入extension与table两个参数，table中包含皮肤数据
---@param extension table?
---@param table table @ 储存所有皮肤信息
skinlist.normalAddSkins = function(extension, table)
    local general = table.name  --武将名字
    local skinname = table.skinname  --皮肤列表，输入一个表，表中按顺序包含所有皮肤的名字
    local kingdom = table.kingdom  --武将所属势力，默认群
    local gender = table.gender | General.Female   --武将性别
    local translation = table.translation   --武将名字的翻译
    local blacklist = table.blacklist
    local blacklistKingdom = table.blacklistKingdom
    if table.extra then --extra为特别的添加方法，用于在重复武将名的时候添加皮肤
        if type(table.extra) ~= "table" then
            table.extra = {table.extra}
        end
        if not table.extra[2] then
            table.extra[2] = kingdom
        end
        local trueName = table.extra[1]
        local head = table.extra[2].."_"..general
        for i = 1, #skinname do
            table[general..tostring(i)] = General(extension, "skin_"..head..tostring(i).."__"..general, kingdom, 0, 0, gender)
            table[general..tostring(i)].hidden = true
        end
        skinlist.skinTable[kingdom][trueName] = skinlist.getAllSkinStr(general, #skinname, nil, head)
        skinlist.skinTrans(general, #skinname, translation, skinname, head)
        if table.extra[3] then
            local extramenbers = table.extra[3]
            for _, g in ipairs(extramenbers) do
                skinlist.skinTable[kingdom][g] = skinlist.getAllSkinStr(general, #skinname, nil, head)
            end
        end
        if blacklist then
            if not blacklistKingdom then
                blacklistKingdom = kingdom
            end
            skinlist.banGeneral(blacklist, kingdom)
        end
    else
        for i = 1, #skinname do
            table[general..tostring(i)] = General(extension, "skin_"..general..tostring(i).."__"..general, kingdom, 0, 0, gender)
            table[general..tostring(i)].hidden = true
        end
        skinlist.skinTable[kingdom][general] = skinlist.getAllSkinStr(general, #skinname)
        skinlist.skinTrans(general, #skinname, translation, skinname)
    end 
end

--获得某些满足特定条件的武将
---@param general string
---@param kingdom string
---@param head string
---@param bottom string
---@param exfunc string
skinlist.getGeneralsByName = function(general, kingdom, head, bottom, exfunc)
    local ans = {}
    for k, g in pairs(Fk.generals) do
        local a = true
        while a do
            a = false
            if g.name:startsWith("skin") then
                break
            end
            if exfunc then
                if not exfunc(g) then
                    break
                end
            end
            if not general or g.trueName == general then
                if not kingdom or g.kingdom == kingdom then
                    if not head or g.name:startsWith(head) then
                        if not bottom or g.name:endsWith(bottom) then
                            table.insertIfNeed(ans, g)
                        end
                    end
                end
            end
        end
    end
    return ans
end



--这个表用于存放所有的武将皮肤字典，读取皮肤时调用的就是这个表格的索引，应当使用函数添加配对
skinlist.skinTable = {
    shu = {
        
    },
    wu = {
        
    },
    wei = {

    },
    qun = {

    },
    jin = {

    },
    god = {

    },
    others = {

    },
}

return skinlist